package org.sai.wxnovel.controller;

import org.sai.wxnovel.service.GenresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
public class CommonController {
    @Autowired
    private GenresService genresService;

    @RequestMapping("/hello.sai")
    public String hello(Integer a, String b, Model model){
        System.out.println(a);
        System.out.println(b);
        model.addAttribute("a",a);
        model.addAttribute("b",b);
        return "nohello";
    }


    @RequestMapping("/hello1.sai")
    public ModelAndView hello(Integer a, String b, ModelAndView model){
        System.out.println(a);
        System.out.println(b);
        model.setViewName("nohello");
        return model;
    }

    @RequestMapping("/index.sai")
    public String index(Model model){
        model.addAttribute("allGenres",genresService.getAllGenres());
        return "index1";
    }

    @RequestMapping("/ajax.sai")
    public String ajax(){
        return "ajax";
    }


    @ResponseBody
    @RequestMapping(value = "/doajax.sai",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public Map<String,Object> doajax(Integer id,String name){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/doajax.sai",method = RequestMethod.POST,produces = "html/text;charset=utf-8")
    public String doajax2(Integer id,String name){
        return "你好"+name;
    }
}
